import {
  AutoIncrement,
  BelongsTo,
  Column,
  DataType,
  ForeignKey,
  HasMany,
  Model,
  PrimaryKey,
  Sequelize,
  Table
} from 'sequelize-typescript'
import { Player, Team } from './a'

export const sequelize = new Sequelize({
  name: 'my_world',
  username: 'misty',
  host: 'localhost',
  dialect: 'mysql',
  password: null,
  dialectOptions: {
    passwordSha1: Buffer.from('36bdbce7890bcce2bb4cda4d2eb29825f6befde8', 'hex'),
  },
  pool: {
    max: 5,
    min: 0
  }
})

@Table({ tableName: 'city' })
export class CityTS extends Model<CityTS> {
  @PrimaryKey
  @AutoIncrement
  @Column(DataType.INTEGER)
  id: number

  @Column(DataType.STRING)
  name: string

  @ForeignKey(() => CountryTS)
  @Column(DataType.STRING)
  countryCode: string

  @BelongsTo(() => CountryTS)
  country: CountryTS

  @Column(DataType.STRING)
  district: string

  @Column(DataType.INTEGER)
  population: number
}

@Table({ tableName: 'country' })
export class CountryTS extends Model<CountryTS> {
  @PrimaryKey
  @Column(DataType.CHAR(3))
  code: string

  @Column(DataType.CHAR(52))
  name: string

  @Column(DataType.ENUM([ 'Asia', 'Europe', 'North America', 'Africa', 'Oceania', 'Antarctica', 'South America' ]))
  continent: string

  @Column(DataType.CHAR(26))
  region: string

  @Column(DataType.FLOAT)
  surfaceArea: number

  @Column(DataType.INTEGER(6))
  indepYear: number

  @Column(DataType.INTEGER)
  population: number

  @HasMany(() => CityTS)
  cities: CityTS[]
}

sequelize.addModels([ CityTS, CountryTS, Team, Player ])